﻿
using ExitGames.Client.Photon;
using Photon.Pun;
using UnityEngine;

public class PlayerInput : MonoBehaviourPun 
{
    public ChessType chessType;
    ChessController chessController;
    void Awake()
    {
        chessController = GameObject.FindObjectOfType<ChessController>();
    }
    void Update()
    {


        switch (chessController.gameState)
        {
            case GameState.Ready:
                break;
            case GameState.Run:
                if (Input.GetMouseButtonDown(0))
                {
                    int x = -1, y = -1;
                    if (chessController.ClimpMousePos(Input.mousePosition, ref x, ref y))
                    {
                        if (photonView.IsMine && PhotonNetwork.IsConnected)
                        {
                            Hashtable table = PhotonNetwork.CurrentRoom.CustomProperties;
                            if ((ChessType)table["步子"] == chessType)
                            {
                                //Step(vector, ChessType.Write);
                                switch (chessType)
                                {
                                    case ChessType.Write:
                                        table["步子"] = ChessType.Black;
                                        break;
                                    case ChessType.Black:
                                        table["步子"] = ChessType.Write;
                                        break;
                                }
                                photonView.RpcSecure("Step", RpcTarget.All, false, x, y, chessType);
                                PhotonNetwork.CurrentRoom.SetCustomProperties(table);
                            }

                        }
                        else
                        {
                            if (!isDown)
                            {
                                Step(x, y, chessType);
                                isDown = true;
                                
                            }
                            
                        }

                    }
                }
                break;
            case GameState.End:

                break;
        }


        //Debug.Log(gameState);
        ListenerDelay();

    }
    float Timer = 0;
    float DelayTimer = 1;
    bool isDown;

    public void ListenerDelay()
    {
        if (isDown)
        {
            Timer += Time.deltaTime;
            if (Timer>DelayTimer)
            {
                isDown = false;
                Timer = 0;
            }
        }
    }

    [PunRPC]
    void Step(int xInt,int yInt, ChessType chessType)
    {
        chessController.CreateChess(xInt,yInt, chessType);
        if (chessController.IsWin((int)chessType))
        {
            if (ChessController.win != null)
            {
                ChessController.win(chessType);
            }
            //endGameUI.WinShowUI(chessType);
            chessController.SetGameState(GameState.End);
        }
    }
}